package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.sc.nft.dao.EcologyHatchExtraRewardsConfigDao;
import com.sc.nft.dao.EcologyHatchExtraRewardsDao;
import com.sc.nft.dao.EcologyHatchExtraRewardsMethodConfDao;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.EcologyHatchExtraRewardsDTO;
import com.sc.nft.entity.dto.EcologyHatchExtraRewardsMethodConfDTO;
import com.sc.nft.entity.dto.EcologyHatchExtraRewardsSaveDTO;
import com.sc.nft.entity.vo.EcologyHatchExtraRewardsAdminVO;
import com.sc.nft.entity.vo.EcologyHatchExtraRewardsConfigAdminVO;
import com.sc.nft.entity.vo.EcologyHatchExtraRewardsMethodConfVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyHatchExtraRewardsConfigService;
import com.sc.nft.service.EcologyHatchExtraRewardsMethodConfService;
import com.sc.nft.service.EcologyHatchExtraRewardsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-09-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchExtraRewardsServiceImpl implements EcologyHatchExtraRewardsService {

    private final EcologyHatchConfDao ecologyHatchConfDao;

    private final EcologyHatchExtraRewardsDao ecologyHatchExtraRewardsDao;

    private final EcologyHatchExtraRewardsConfigDao ecologyHatchExtraRewardsConfigDao;


    private final EcologyHatchExtraRewardsMethodConfService ecologyHatchExtraRewardsMethodConfService;

    private final EcologyHatchExtraRewardsMethodConfDao ecologyHatchExtraRewardsMethodConfDao;


    /**
     * 编辑额外奖励
     * @param ecologyHatchExtraRewardsSaveDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEcologyHatchExtraRewards(EcologyHatchExtraRewardsSaveDTO ecologyHatchExtraRewardsSaveDTO){
        EcologyHatchConf ecologyHatchConfInDb = ecologyHatchConfDao.getById(ecologyHatchExtraRewardsSaveDTO.getEcologyHatchId());
        Assert.notNull(ecologyHatchConfInDb, () -> new GlobalRunTimeException("生态熔炼配置不存在"));

        //生态额外奖励表Id
        Long extraRewardsId = ecologyHatchExtraRewardsSaveDTO.getId();
        if (Objects.isNull(extraRewardsId)) {
            EcologyHatchExtraRewards ecologyHatchExtraRewards = BeanUtil.copyProperties(ecologyHatchExtraRewardsSaveDTO, EcologyHatchExtraRewards.class);
            ecologyHatchExtraRewards.setEcologyHatchId(ecologyHatchConfInDb.getId());
            ecologyHatchExtraRewards.insert();
            extraRewardsId = ecologyHatchExtraRewards.getId();

        }else {
            Assert.isFalse(ecologyHatchConfInDb.getStatus(),() -> new GlobalRunTimeException("请先下架熔炼配置"));
            Assert.isTrue(ecologyHatchExtraRewardsDao.updateEcologyHatchExtraRewardsById(ecologyHatchExtraRewardsSaveDTO.getId(),
                    ecologyHatchExtraRewardsSaveDTO.getStatus(),ecologyHatchExtraRewardsSaveDTO.getExtraRuleRemarkImg()), () -> new GlobalRunTimeException("修改生态额外配置奖励失败"));
        }
        //编辑熔炼奖励方式关联表
        ecologyHatchExtraRewardsMethodConfService.editEcologyHatchExtraRewardsConfigMethodConf(ecologyHatchExtraRewardsSaveDTO.getEcologyHatchId(),extraRewardsId,
                ecologyHatchExtraRewardsSaveDTO.getEcologyHatchExtraRewardsMethodConfDTOS());
        //编辑熔炼上限道具关联表
        /*ecologyHatchExtraRewardsConfigService.editEcologyHatchExtraRewardsConfig(ecologyHatchExtraRewardsSaveDTO.getEcologyHatchId()
                ,extraRewardsId,ecologyHatchExtraRewardsSaveDTO.getStatus(),ecologyHatchExtraRewardsSaveDTO.getEcologyHatchExtraRewardsConfigSaveDTOS());*/

    }

    /**
     * 额外配置奖励详情
     * @param ecologyHatchExtraRewardsDTO
     * @return
     */
    @Override
    public EcologyHatchExtraRewardsAdminVO findEcologyHatchExtraRewards(EcologyHatchExtraRewardsDTO ecologyHatchExtraRewardsDTO){
        EcologyHatchExtraRewards ecologyHatchExtraRewards = ecologyHatchExtraRewardsDao.getOneByEcologyHatchId(ecologyHatchExtraRewardsDTO.getEcologyHatchId());
        if (Objects.isNull(ecologyHatchExtraRewards)){
            return null;
        }
        EcologyHatchExtraRewardsAdminVO ecologyHatchExtraRewardsVO = BeanUtil.copyProperties(ecologyHatchExtraRewards, EcologyHatchExtraRewardsAdminVO.class);

        List<EcologyHatchExtraRewardsMethodConf> ecologyHatchExtraRewardsMethodConfList = ecologyHatchExtraRewardsMethodConfDao.findListByEcologyHatchId(ecologyHatchExtraRewardsVO.getEcologyHatchId());
        List<EcologyHatchExtraRewardsMethodConfVO> ecologyHatchExtraRewardsMethodConfVOS = BeanUtil.copyToList(ecologyHatchExtraRewardsMethodConfList, EcologyHatchExtraRewardsMethodConfVO.class);
        ecologyHatchExtraRewardsVO.setEcologyHatchExtraRewardsMethodConfVOS(ecologyHatchExtraRewardsMethodConfVOS);
        for (EcologyHatchExtraRewardsMethodConfVO ecologyHatchExtraRewardsMethodConfVO : ecologyHatchExtraRewardsMethodConfVOS) {

            List<EcologyHatchExtraRewardsConfig> ecologyHatchExtraRewardsConfigs = ecologyHatchExtraRewardsConfigDao.findAdminListByMethodId(ecologyHatchExtraRewardsMethodConfVO.getId());
            List<EcologyHatchExtraRewardsConfigAdminVO> ecologyHatchExtraRewardsConfigAdminVOS = BeanUtil.copyToList(ecologyHatchExtraRewardsConfigs, EcologyHatchExtraRewardsConfigAdminVO.class);
            ecologyHatchExtraRewardsMethodConfVO.setEcologyHatchExtraRewardsConfigList(ecologyHatchExtraRewardsConfigAdminVOS);
        }
        return ecologyHatchExtraRewardsVO;

    }


    /**
     * 生成奖励配置方式 脚本
     */
    public void extraRewardsMethodScript(){
        List<EcologyHatchExtraRewards> ecologyHatchExtraRewardsList = ecologyHatchExtraRewardsDao.getAll();

        List<EcologyHatchExtraRewards> ecologyHatchExtraRewardsList_ = new ArrayList<EcologyHatchExtraRewards>();
        //根据熔炼id 去重
        ecologyHatchExtraRewardsList_ =  ecologyHatchExtraRewardsList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(EcologyHatchExtraRewards::getEcologyHatchId))), ArrayList::new));

        for (EcologyHatchExtraRewards ecologyHatchExtraRewards : ecologyHatchExtraRewardsList_) {

            List<EcologyHatchExtraRewardsMethodConf> ecologyHatchExtraRewardsMethodConfList = ecologyHatchExtraRewardsMethodConfDao.findListByEcologyHatchId(ecologyHatchExtraRewards.getEcologyHatchId());
            if (CollectionUtils.isEmpty(ecologyHatchExtraRewardsMethodConfList)){
                EcologyHatchExtraRewardsMethodConf ecologyHatchExtraRewardsMethodConf = new EcologyHatchExtraRewardsMethodConf();
                ecologyHatchExtraRewardsMethodConf.setExtraRewardsId(ecologyHatchExtraRewards.getId());
                ecologyHatchExtraRewardsMethodConf.setEcologyHatchId(ecologyHatchExtraRewards.getEcologyHatchId());
                ecologyHatchExtraRewardsMethodConf.setProbability(BigDecimal.ZERO);
                ecologyHatchExtraRewardsMethodConf.insert();
            }
        }
        //List<Long> extraRewardsIds = ecologyHatchExtraRewardsList.stream().map(EcologyHatchExtraRewards::getId).collect(Collectors.toList());

        List<EcologyHatchExtraRewardsMethodConf> ecologyHatchExtraRewardsMethodConfList = ecologyHatchExtraRewardsMethodConfDao.findAll();
        for (EcologyHatchExtraRewardsMethodConf ecologyHatchExtraRewardsMethodConf : ecologyHatchExtraRewardsMethodConfList) {
            List<EcologyHatchExtraRewardsConfig> ecologyHatchExtraRewardsConfigList = ecologyHatchExtraRewardsConfigDao.findListByExtraRewardsId(ecologyHatchExtraRewardsMethodConf.getExtraRewardsId());
            for (EcologyHatchExtraRewardsConfig ecologyHatchExtraRewardsConfig : ecologyHatchExtraRewardsConfigList) {
                ecologyHatchExtraRewardsConfigDao.updateMethodId(ecologyHatchExtraRewardsConfig.getExtraRewardsId(),ecologyHatchExtraRewardsMethodConf.getId());
            }
        }
    }



}
